// pages/album/album.js
const {getAlbumDetail} = require('../../service/recommend')

Page({
  data: {
    album: {},
    scrollY: 0,
    songs: []
  },
  onLoad: async function (options) {
    wx.showLoading({title: '数据获取中'})
    const album = JSON.parse(decodeURIComponent(options.album))
    const res = (await getAlbumDetail(album.id)).data
    const songs = this.convertArray(res.playlist.subscribers)
    this.setData({album, songs}, () => wx.hideLoading())
  },
  // 转换数组，做性能优化
  convertArray(arr) {
    if (!Array.isArray(arr) || !arr.length) return
    return arr.map(item => ({
      name: item.nickname,
      al: {name: item.nickname},
      gender: item.gender,
      userId: item.userId,
      city: item.city,
      avatarUrl: item.avatarUrl,
      backgroundUrl: item.backgroundUrl
    }))
  },
  onPageScroll(e) {
    this.setData({
      scrollY: e.scrollTop
    })
  },
})